package com.bitstu.QRcode;
import com.google.zxing.*;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.nio.file.Path;
import java.util.Collection;
import java.util.HashMap;

public class create {
    public static void main(String[] args) {
        int width=300;
        int height=300;
        String format="jpeg";
        String content="https://blog.csdn.net/champion_man/article/details/80706759";


        //定义二维码的参数
        HashMap hints=new HashMap();
        hints.put(EncodeHintType.CHARACTER_SET,"utf-8");
        hints.put(EncodeHintType.ERROR_CORRECTION,ErrorCorrectionLevel.M);
        hints.put(EncodeHintType.MARGIN,2);

        try {
            BitMatrix matrix=new MultiFormatWriter().encode(content,BarcodeFormat.QR_CODE,width,height,hints);

            Path file1=new File("E:/code/img5.png").toPath();
            MatrixToImageWriter.writeToPath(matrix,format,file1);
            BufferedImage two=ImageIO.read(new File(file1.toString()));
            Graphics2D g=two.createGraphics();
            BufferedImage logo=ImageIO.read(new File("E:/code/4.jpg"));
            int logoWidth=logo.getWidth()>two.getWidth()*2/8?(two.getWidth()*2/8):logo.getWidth();
            int logoHeight=logo.getHeight()>two.getHeight()*2/8?(two.getHeight()*2/8):logo.getHeight();
            int x=(two.getWidth()-logoWidth)/2;
            int y=(two.getHeight()-logoHeight)/2;
            g.drawImage(logo,x,y,logoWidth,logoHeight,null);
            g.drawRoundRect(x,y,logoWidth,logoHeight,15,15);
            g.setStroke(new BasicStroke(2));
            g.setColor(Color.YELLOW);
            g.drawRect(x,y,logoWidth,logoHeight);
            g.dispose();
            logo.flush();
            two.flush();
            ImageIO.write(two,format,new File("E:/code/img4.png"));

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
